/***************************************************************************
 *   Copyright (C) 2009 by maarten,,,   *
 *   maarten@maarten-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef OBJECTVERTEX_H
#define OBJECTVERTEX_H

#include <math.h>

namespace draw
{
    class FloatMatrix;
}

namespace object {

/**
	@author maarten,,, <maarten@maarten-laptop>
*/
class Vertex{
public:
    Vertex(float x=0, float y=0, float z=0, float h = 1):
                    m_x(x), m_y(y), m_z(z),m_h(h){}
    ~Vertex(){;}
    
    inline void setValues(float x, float y, float z, float h)
    {
        m_x = x;
        m_y = y;
        m_z = z;
        m_h = h;
    };

    inline float x(){return m_x;}
    inline float y(){return m_y;}
    inline float z(){return m_z;}
    inline float h(){return m_h;}
    
    inline void setX(float val){m_x = val;vLenght = sqrt((m_x * m_x) +
(m_y * m_y) + (m_z * m_z) + (m_h * m_h));}
    inline void setY(float val){m_y = val; vLenght = sqrt((m_x * m_x) +
(m_y * m_y) + (m_z * m_z) + (m_h * m_h));}
    inline void setZ(float val){m_z = val;vLenght = sqrt((m_x * m_x) +
(m_y * m_y) + (m_z * m_z) + (m_h * m_h));}
    inline void setH(float val){m_h = val;vLenght = sqrt((m_x * m_x) +
(m_y * m_y) + (m_z * m_z) + (m_h * m_h));}
    
    void print();
    void normalize();
    void unNormalize();
    
    inline float getLength(){return vLenght;}

    inline Vertex* vecProd(Vertex * b)
    {
        return new Vertex(m_y * b->z() - m_z * b->y(), 
                          m_z * b->x() - m_x * b->z(),
                          m_x * b->y() - m_y * b->x(),
                          m_h * b->h() - m_h * b->h());
    }
    
    inline float dotProd(Vertex * b){
      return (m_x*b->x() + m_y*b->y() + m_z*b->z());
    }
    
    inline Vertex* negative(){return new Vertex(-m_x,-m_y,-m_z,-m_h);}
    
    inline Vertex* minus(Vertex * b){
      return new Vertex(m_x - b->x(),m_y - b->y(), m_z - b->z(), m_h- b->h());
    }
    
    inline Vertex* plus(Vertex * b){
      return new Vertex(m_x + b->x(), m_y + b->y(), m_z + b->z(), m_h+b->h());
    }
    
    inline Vertex* scaled(float factor){
        return new Vertex(m_x*factor , m_y*factor , m_z*factor );
    }
    
    void transform(draw::FloatMatrix * val);
    
    inline float getNorm()
    {
      return (float)sqrt((m_x * m_x) + (m_y * m_y) + (m_z * m_z));
    }
    
    inline Vertex* getGlossy(){
      Vertex * newVertex = new Vertex();
      if(m_x <= m_y && m_x <= m_z){
        newVertex->setX(1);
        newVertex->setY(m_y);
        newVertex->setZ(m_z);
        newVertex->setH(0);
      }
      else if(m_y <= m_x && m_y <= m_z){
        newVertex->setX(m_x);
        newVertex->setY(1);
        newVertex->setZ(m_z);
        newVertex->setH(0);
      }
      else if(m_z <= m_x && m_z <= m_y){
        newVertex->setX(m_x);
        newVertex->setY(m_y);
        newVertex->setZ(1);
        newVertex->setH(0);
      }
      
      return newVertex;
    }

private:
    float m_x;
    float m_y;
    float m_z;
    float m_h;
    
    float vLenght;
};

}

#endif
